package com.study.jyg.rxjava2study.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.utils.AdapterInit;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class RecyclerViewActivity extends BasicActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.btnTest)
    Button btnTest;
    @BindView(R.id.btnOther)
    Button btnOther;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private BaseQuickAdapter<String, BaseViewHolder> adapter;
    private long time;

    @Override
    protected int getLayout() {
        return R.layout.activity_recycler_view;
    }



    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(v -> finish());
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            list.add(String.valueOf(i));
        }
        time = System.currentTimeMillis();
        adapter = AdapterInit.initRecyclerView(recyclerView, R.layout.item_recyclerview_textview, list, new LinearLayoutManager(mContext), (helper, item) -> {
            Log.e("adapter", "onbind:"+ helper.getAdapterPosition());
            helper.setText(R.id.tvItem, String.valueOf(helper.getAdapterPosition()));
        }, (adapter1, view, position) -> {
            long clickTime = System.currentTimeMillis();
            if ( clickTime - time > 250){
                adapter.getData().remove(position);
                adapter.notifyItemRemoved(position);
                adapter.notifyItemRangeRemoved(position, adapter.getData().size()- position);
                time = clickTime;
            }
        });
        btnTest.setOnClickListener(v -> {
            int start = 5;
            int itemcount = adapter.getData().size();
            for (int i = 0; i < start; i++) {
                adapter.getData().remove(0);
                adapter.notifyItemRemoved(0);
            }
            adapter.notifyItemRangeChanged(0, itemcount);
        });
        btnOther.setOnClickListener(v -> {//
            startActivity(GridLayoutManagerActivity.class);
        });

    }
}
